package com.qztc.controller;

import com.qztc.hire_end_dto.dto.MemberPrivilege;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

import static com.qztc.config.GlobalConfig.SERVICE_URL;

@RestController
@RequestMapping("/consumer/memberPrivilege")
@Tag(name = "消费者端会员权限管理接口")
public class MemberPrivilegeControllerByRestTemplate {
    @Autowired
    private RestTemplate restTemplate;

    @Operation(summary = "查询所有会员权限")
    @GetMapping("/getAll")
    public String getAll() {
        return restTemplate.getForObject(SERVICE_URL+"/memberPrivilege/getAll", String.class);
    }

    @Operation(summary = "查询具体会员权限")
    @GetMapping("/getbymemberPrivilegeId")
    public String getbymemberPrivilegeId(@RequestParam Integer memberPrivilegeId) {
        return restTemplate.getForObject(SERVICE_URL+"/memberPrivilege/getbymemberPrivilegeId?memberPrivilegeId="+memberPrivilegeId, String.class);
    }

    @Operation(summary = "修改会员权限")
    @PutMapping("/update")
    public String update(@RequestBody MemberPrivilege memberPrivilege) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<MemberPrivilege> entity = new HttpEntity<>(memberPrivilege, headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/memberPrivilege/update", HttpMethod.PUT,entity, String.class);
        return result.getBody();
    }

    @Operation(summary = "增加会员权限")
    @PostMapping("/add")
    public String add(@RequestBody MemberPrivilege memberPrivilege) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<MemberPrivilege> entity = new HttpEntity<>(memberPrivilege, headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/memberPrivilege/add", HttpMethod.POST,entity, String.class);
        return result.getBody();
    }

    @Operation(summary = "删除会员权限")
    @DeleteMapping("/delete")
    public String delete(@RequestBody MemberPrivilege memberPrivilege) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<MemberPrivilege> entity = new HttpEntity<>(memberPrivilege, headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/memberPrivilege/delete", HttpMethod.DELETE,entity, String.class);
        return result.getBody();
    }

    @Operation(summary = "批量删除会员权限")
    @DeleteMapping("/deleteMany")
    public String deleteMany(@RequestBody List<Integer> memberPrivilegeIds) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<List<Integer>> entity = new HttpEntity<>(memberPrivilegeIds, headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/memberPrivilege/deleteMany", HttpMethod.DELETE,entity, String.class);
        return result.getBody();
    }

    @Operation(summary = "批量增加会员权限")
    @PostMapping("/addMany")
    public String addMany(@RequestBody List<MemberPrivilege> memberPrivileges) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<List<MemberPrivilege>> entity = new HttpEntity<>(memberPrivileges, headers);
        ResponseEntity<String> result = restTemplate.exchange(SERVICE_URL+"/memberPrivilege/addMany", HttpMethod.POST,entity, String.class);
        return result.getBody();
    }
}
